#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/shm.h>
#include <pthread.h>

#include "head.h"
 
int main(int argc, char *argv[])
{
    void *shmptr = NULL;
    struct shared_struct *shared = NULL;
    int shmid;
    key_t key;

    char buffer[50]; /* 8192bytes, saved from stdin */
    
    sscanf(argv[1], "%x", &key);

    printf("write: IPC key = %x\n", key);

    shmid = shmget((key_t)key, sizeof(int) * 3 + TEXT_NUM*sizeof(struct shared_struct) , 0666|PERM);
    if (shmid == -1) {
        ERR_EXIT("write: shmget()");
    }

    shmptr = shmat(shmid, 0, 0);
    if(shmptr == (void *)-1) {
        ERR_EXIT("write: shmat()");
    }
    printf("write: shmid = %d\n", shmid);
    printf("write: shared memory attached at %p\n", shmptr);
    printf("write precess ready ...\n");
    printf("hello world\n");

    int * lock = (int*)shmptr;
    int * count_ptr = (int *)(shmptr + 4);

    struct shared_struct* head = (struct shared_struct*)(shmptr + 8);
    
    while (1) {
        while (*lock == 1) {
            sleep(1); /* message not read yet, waiting ... */ 
        }
 
        printf("Enter some text: ");
	
	int i = 0;
	while((buffer[i] = getchar()) != '\n' && i < 50){
	    i ++;
	}
	buffer[i] = '\0';
        //scanf("%s", buffer);
        //strncpy(shared->mtext, buffer, TEXT_SIZE);
        printf("shared buffer: %s\n",buffer);

        //insert(head, count_ptr, TEXT_NUM, buffer, rand()%15);
        
        *lock = 1;  /* message prepared */
 
        if(strncmp(buffer, "end", 3) == 0) {
            break;
        } else if(strncmp(buffer, "delete node", 11) == 0) {
	    char * str = NULL;
	    str = (char *)malloc(sizeof(char));
	    strncpy(str, buffer+12, strlen(buffer)-12);
	    int index = atoi(str);
	    removeAtIndex(head, TEXT_NUM, index, count_ptr);	
	} else if(strncmp(buffer, "replace node", 12) == 0) {
	    int indexs[5] = {'\0'};
	    int index = -1;
	    char * name = NULL;
	    name = (char *)malloc(sizeof(char));
	    int id = -1;

	    int j;
	    int count = 0;
	    for(j = 0; j < strlen(buffer); j++) {
	        if(buffer[j] == ' '){
		    indexs[count++] = j+1;
		}
	    }

	    for(j = 0; j < count; j++) {
		char * str = NULL;
		str = (char *)malloc(sizeof(char));
		if(j != count - 1) {
		    strncpy(str, buffer+indexs[j], indexs[j+1] - indexs[j] - 1);
		} else {
		    strncpy(str, buffer+indexs[j], strlen(buffer) - indexs[j]);
		}

		if(j == 1) {
		    index = atoi(str);
		} else if (j == 3) {
		    strcpy(name, str);
		} else if (j == 4) {
		    id = atoi(str);
		}

		printf("str = %s\n", str);
	    }

	    replace(head, TEXT_NUM, index, name, id);
	} else if(strncmp(buffer, "insert", 6) == 0) {
	    int indexs[2] = {'\0'};
	    char * name = NULL;
	    name = (char *)malloc(sizeof(char));
	    char * str = NULL;
	    str = (char *)malloc(sizeof(char));

	    int j;
	    int count = 0;
	    for(j = 0; j < strlen(buffer); j++){
		if(buffer[j] == ' '){
		    indexs[count++] = j+1;
		}
	    }

	    strncpy(name, buffer+indexs[0], indexs[1]-indexs[0]-1);
	    strncpy(str, buffer+indexs[1], strlen(buffer)-indexs[1]);
	    int id = atoi(str);
	    insert(head, count_ptr, TEXT_NUM, name, id);
	} else if (strncmp(buffer, "search id", 9) == 0) {
	    char * str = NULL;
	    str = (char *)malloc(sizeof(char));
	    strncpy(str, buffer+10, strlen(buffer)-10);
	    int id = atoi(str);
	    struct shared_struct * student = NULL;
	    student = searchList(head, TEXT_NUM, id, count_ptr);
	    
	    if(student == NULL) {
		printf("\nNot found!\n\n");
	    } else {
		printf("\nStudent name: %s\n\n", student->name);
	    }
	}
    }
       /* detach the shared memory */
    if(shmdt(shmptr) == -1) {
        ERR_EXIT("write: shmdt()");
    }

    exit(EXIT_SUCCESS);
}
